const express = require('express');
const ipAddress = process.env.MOCK_HOST || '127.0.0.1';
const port = process.env.MOCK_PORT || 6002;

const app = express();

// allow custom header and CORS
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Content-Type, Content-Length, Authorization, Accept, X-Requested-With, x-locale',
  );
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

  if (req.method === 'OPTIONS') {
    res.sendStatus(200); /* 让options请求快速返回 */
  } else {
    next();
  }
});

require('./auth').mock(app);

// eslint-disable-next-line no-console
app.listen(port, ipAddress, () => console.log(`Mock app listening on ${ipAddress}:${port}!`));
